package com.qianfeng.yime;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qianfeng.yime.util.CommonUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

/**
 * Created by IDEA
 * User : SL
 * on  2015/3/12.
 * 19:33
 */

/**
 * 意见反馈界面
 */
public class SuggestionActivity extends BaseActivity implements TextWatcher {

    private EditText suggestionContext;
    private TextView textCount;
    private EditText phone;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_suggestion);
        setTitle("意见反馈");

        suggestionContext = (EditText) findViewById(R.id.edit_suggestion_context);
        textCount = (TextView) findViewById(R.id.text_suggestion_text_count);
        phone = (EditText) findViewById(R.id.edit_suggestion_user_phone);
        Button btnSend = (Button) findViewById(R.id.btn_suggestion_send);

        btnSend.setOnClickListener(this);//发送按钮的点击监听

        suggestionContext.addTextChangedListener(this);
    }

    @Override
    public void onClick(View v) {
        String str = suggestionContext.getText().toString();//意见与反馈内容
        String string = phone.getText().toString();
        long phoneNum = 0;//联系手机号
        if (string.length() > 10 && str.length() > 0) {
            if (string.startsWith("13") || string.startsWith( "18") || string.startsWith("15") || string.startsWith("17")){
                phoneNum = Long.parseLong(string);
            }else {
                CommonUtils.toast(this, "输入不合法");
                return;
            }
        }else {
            CommonUtils.toast(this, "输入不合法");
            return;
        }
        String url = Constants.SUGGESTION_URL;
        LinkedList<NameValuePair> pairs = new LinkedList<NameValuePair>();
        pairs.add(new BasicNameValuePair("tel",Long.toString(phoneNum)));
        pairs.add(new BasicNameValuePair("txt",str));
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter(pairs);
        super.onClick(v);
        int id = v.getId();
        switch (id){
            case R.id.btn_suggestion_send:
                httpUtils.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String result = responseInfo.result;
                        System.out.println("result = " + result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            int code = jsonObject.getInt("code");
                            if (code == 200) {
                                CommonUtils.toast(SuggestionActivity.this, "提交成功");
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        System.out.println("msg = " + msg);
                    }
                });
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
//        textCount.setText(Integer.toString(count) + "/500");
    }

    @Override
    public void afterTextChanged(Editable s) {
        int length = s.length();
        textCount.setText(Integer.toString(length) + "/500");
    }
}